<%@page import="net.sf.json.JSONArray"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="java.sql.ResultSet"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="database" scope="request" class="accessibleworldsdatabase.AccessibleWorldsDatabaseJavaBeans"/>
<jsp:useBean id="des" scope="request" class="accessibleworldsdes.AccessibleWorldsDESJavaBeans"/>
<%try {

String accessCode = request.getParameter("accessCode");

if(!(accessCode==null || "".equals(accessCode))) {
	
    accessCode = des.decryptString(session.getId(), accessCode);
    accessCode = URLDecoder.decode(accessCode, "UTF-8");
    
    if(des.getAndroidAccessCode().equals(accessCode) || des.getComputerAccessCode().equals(accessCode)) {
    	
    	String reportID = request.getParameter("reportID");
    	
    	if(!(reportID == null || "".equalsIgnoreCase(reportID))) {
    		
    		reportID = des.decryptString(session.getId(), reportID);
    		reportID = URLDecoder.decode(reportID, "UTF-8");
    		
    		database.connect(des.decryptString(des.getServiceSecretKey(), database.getEncryptedPassword()));
    	
    		String SQL =
    		"SELECT"+
    			" Comments.Comment_id"+
    			",Users.Email"+
    			",Comments.description"+
    			",Comments.rating"+
				" FROM Comments"+
    				" INNER JOIN Comment_Report ON"+
    					" Comments.Comment_id = Comment_Report.Comment_id" +
    				" INNER JOIN Users ON"+
    					" Users.User_id = Comments.user_id" +
				" WHERE "+
    				" Comments.rating > -5" +
    				" AND Comments.visible = 1" +
					" AND Comment_Report.Report_id = "+reportID;
		
		    ResultSet resultSet = database.execSQL(SQL);

		    String output = URLEncoder.encode("OK","UTF-8");
		    
		    while(resultSet.next()) {
		    	JSONArray jsonArray = new JSONArray();
		    	jsonArray.add(resultSet.getString("Comments.Comment_id"));
		    	jsonArray.add(resultSet.getString("Users.Email"));
		    	jsonArray.add(resultSet.getString("Comments.description"));
		    	jsonArray.add(resultSet.getString("Comments.rating"));
				output = output+" "+URLEncoder.encode(jsonArray.toString(),"UTF-8");
		    }
		
	        output = des.encryptString(session.getId(),output);
	        out.print(output);
	        
		    database.closeConnection();
    	}
    }
}
} catch (Exception ex) {}%>